<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
	<head>
		<th:block th:include="include :: header('选择虚拟用况接口')" />
		<link th:href="@{/css/common.css}" rel="stylesheet"/>
		<link th:href="@{/css/choose_data_map.css}" rel="stylesheet"/>
	</head>
	<body>
		<div class="wrapper wrapper-content field">
			<form id="form-virtualUsage">
				<div class="content">
					<input type="hidden" name="interfaceId" id="interfaceId" th:value="${virtualUsageGroup.interfaceId}">
					<input type="hidden" name="groupId" id="groupId" th:value="${virtualUsageGroup.id}">
					<table class="table table-hover" style = "margin-top:10px;">
						<tbody></tbody>
					</table>
				</div>
			</form>
		</div>
	</body>
	<th:block th:include="include :: footer"/>
	<script th:inline="javascript">
		var prefix = ctx + "virtualUsageGroupDetail/virtualUsageGroupDetail";
		var virtualUsageList = [[${virtualUsageList}]];
		var virtualUsageGroupDetails = [[${virtualUsageGroupDetails}]]
		$(function () {
			loadInterface();
		})
		
		function loadInterface() {
			var data = {};
			if (virtualUsageList && virtualUsageList.length > 0) {
				if (virtualUsageGroupDetails && virtualUsageGroupDetails.length > 0) {
					$("#virtualUsageSelected",window.parent.document).text(virtualUsageGroupDetails.length);
					for (var i=0; i < virtualUsageList.length; i++) {
						for (var j=0; j<virtualUsageGroupDetails.length; j ++) {
							if (virtualUsageList[i].id == virtualUsageGroupDetails[j].virtualUsageId) {
								virtualUsageList[i].selected = true;
								continue;
							}
						}
					}
				} else {
					$("#virtualUsageSelected",window.parent.document).text(0);
				}
			}
			data.list = virtualUsageList || [];
			var paramsTplHtml = $("#paramsTpl").html();
			laytpl(paramsTplHtml).render(data, function (html) {
				$("table tbody").append(html);
			});
		}

		function checkboxClick() {
			var count = 0;
			$('table').find(':checkbox').each(function(){
				if ($(this).is(":checked")) {
					count ++;
				}
			});
			$("#virtualUsageSelected",window.parent.document).text(count);
		}

		function submitHandler() {
			var count = $("#virtualUsageSelected",window.parent.document).text();
			if (count > 0) {
				var config = {
					url: prefix + "/saveVirtualUsage",
					type: "post",
					async: false,
					dataType: "json",
					data: $('#form-virtualUsage').serialize(),
					beforeSend: function () {
						$.modal.loading("正在处理中，请稍后...");
						$.modal.disable();
					},
					success: function(result) {
						if (result.code == web_status.SUCCESS) {
							$.modal.close();
						} else if (result.code == web_status.WARNING) {
							$.modal.alertWarning(result.msg)
						}  else {
							$.modal.alertError(result.msg);
						}
						$.modal.closeLoading();
					}
				};
				$.ajax(config)
			} else {
				$.modal.alertWarning("请选择接口");
			}
		}
	</script>
	<script id="paramsTpl" type="text/template">
		{{# layui.each(d.list, function(index, a){ }}
		<tr>
			{{# if(a.selected) { }}
			<td><input name="virtualUsageIds" checked onclick="checkboxClick()" type="checkbox" value="{{a.id}}"></td>
			{{# } else { }}
			<td><input name="virtualUsageIds" onclick="checkboxClick()" type="checkbox" value="{{a.id}}"></td>
			{{# } }}
			<td>{{a.slaveInterfaceName}}</td>
		</tr>
		{{# }); }}
	</script>
</html>
